<template>
  <div class="login-container">
    <div class="login-form-wrap">
      <div class="login-head">
        <div class="logo"></div>
      </div>
      <el-form class="login-form" :rules="rules" ref="form" :model="form">
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input    v-model="form.code" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item  prop="isAgree">
          <el-checkbox v-model="form.isAgree" label="我已阅读并同意用户协议和隐私条款" name="type"></el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button class="login-btn" type="primary" @click="onSubmit" :loading = 'loading'>登陆</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'LoginIndex',
  data () {
    return {
      // 表单验证
      rules: {
        // 手机验证
        // trigger
        mobile: [
          { required: true, message: '请输入手机号码', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入合法的手机号', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'change' },
          { pattern: /^\d{6}$/, message: '请输入正确的验证码', trigger: 'change' }
        ],
        isAgree: [
          {
            validator: (rule, value, callback) => {
              if (value) {
                // 用户同意
                callback()
              } else {
                callback(new Error('必须要同意!!!!'))
              }
            },
            trigger: 'change'
          }
        ]
      },
      // 1.定义初始值
      form: {
        mobile: '', // 手机号
        code: '', // 验证码
        isAgree: false // 是否同意
      },
      loading: false
    }
  },
  methods: {
    // 2.确定登录
    onSubmit () {
      // console.log('qqq' + this.$refs.form)
      // 有效性检验： 通过给表单添加ref属性，获取表单元素，调用validate
      this.$refs.form.validate(valid => {
        // 验证通过
        if (valid) {
          this.login()
        }
      })
    },
    async login () {
      this.loading = true
      // 用户登录
      // 1.获取表单中的手机号， 密码
      // console.log(this.form.mobile, this.form.code, this.form.isAgree)
      // 2.有效性检验
      // 3.axios 请求，带上参数
      try {
        const result = await this.$http({
          method: 'POST',
          url: 'mp/v1_0/authorizations',
          data: {
            mobile: this.form.mobile,
            code: this.form.code
          }
        })
        this.$message({
          message: '恭喜你，登录成功',
          type: 'success'
        })
        this.loading = false
        // 2. 保存本地token
        localStorage.setItem('tokenStr', result.data.data.token)
        this.$router.push('/')
      } catch (err) {
        //  (2) 失败 -> 提示
        console.log(err)
        // alert('error')
        this.$message({
          message: '登陆失败',
          type: 'error'
        })
        this.isLoading = false
      }
    }
  }
}
</script>
<style scoped lang="less">
// 铺满整屏
.login-container {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction:column;
  justify-content: center;
  align-items: center;
  background: url("../../assets/login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    min-width: 300px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("../../assets/logo_index.png") no-repeat;
        background-size: contain;
      }
    }
    .login-form {
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
